package com.bawei.mall.product.controller;

import com.bawei.common.core.domain.R;
import com.bawei.common.core.utils.poi.ExcelUtil;
import com.bawei.common.core.web.controller.BaseController;
import com.bawei.common.core.web.domain.AjaxResult;
import com.bawei.common.core.web.page.TableDataInfo;
import com.bawei.common.log.annotation.Log;
import com.bawei.common.log.enums.BusinessType;
import com.bawei.common.security.annotation.RequiresPermissions;
import com.bawei.mall.product.domain.MallProductInfo;
import com.bawei.mall.product.domain.reponse.ProductDetailsResponse;
import com.bawei.mall.product.domain.reponse.ProductInfoResponse;
import com.bawei.mall.product.domain.request.ProductInfoRequest;
import com.bawei.mall.product.service.IMallProductInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品信息Controller
 *
 * @author DongZeLiang
 * @date 2022-09-19
 */
@RestController
@RequestMapping("/info")
@Api(tags = "商品维护 - API")
public class MallProductInfoController extends BaseController
{
    @Autowired
    private IMallProductInfoService mallProductInfoService;


    /**
     * 查询商品信息列表
     */
    @RequiresPermissions("product:info:list")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public TableDataInfo list(MallProductInfo mallProductInfo)
    {
        startPage();
        List<MallProductInfo> list = mallProductInfoService.selectMallProductInfoList(mallProductInfo);
        return getDataTable(list);
    }


    @GetMapping("/productList")
    public R<List<MallProductInfo>> syncLists(MallProductInfo mallProductInfo) {

        PageHelper.startPage(mallProductInfo.getPageNum(),mallProductInfo.getPageSize());
            List<MallProductInfo> list = mallProductInfoService.selectMallProductInfoList(mallProductInfo);
            PageInfo<MallProductInfo> info = new PageInfo<>(list);
            /*MallProductInfoVo mallProductInfoVo = new MallProductInfoVo();
            mallProductInfoVo.setProductList(info.getList());
            mallProductInfoVo.setLength(info.getList().size());*/
        return R.ok(info.getList());
    }

    /*@GetMapping("/detail/{id}")
    public R<ProductDetailsResponse> detail(@PathVariable("id") @ApiParam(value = "商品ID", required = true) Long id) {
        ProductDetailsResponse product = mallProductInfoService.selectProductDetailsById(id);
        MallProductBrandInfo brand = mallProductBrandInfoService.selectMallProductBrandInfoById(Long.valueOf(mallProductInfo.getBrandId()));
        return R.ok(product);
    }*/


    /**
     * 查询商品信息总条数
     */
    @RequiresPermissions("product:info:list")
    @PostMapping("/count")
    public R<Long> count(@RequestBody MallProductInfo mallProductInfo)
    {
        return R.ok(mallProductInfoService.selectMallProductInfoCount(mallProductInfo));
    }



    /**
     * 导出商品信息列表
     */
    @RequiresPermissions("product:info:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallProductInfo mallProductInfo)
    {
        List<MallProductInfo> list = mallProductInfoService.selectMallProductInfoList(mallProductInfo);
        ExcelUtil<MallProductInfo> util = new ExcelUtil<MallProductInfo>(MallProductInfo.class);
        util.exportExcel(response, list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @RequiresPermissions("product:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mallProductInfoService.selectMallProductInfoById(id));
    }

    /**
     * 上架商品
     */
    /*@GetMapping(value = "/upload/{id}")
    @ApiOperation("上架商品")
    public R uploadInfo(@PathVariable("id") Long id,String isUpload)
    {
        mallProductInfoService.uploadInfo(id, isUpload);
        return R.ok();
    }*/
    @GetMapping("/UpdateStats")
    @ApiOperation("上架商品")
    public R uploadInfo(@RequestParam(value = "id") Long id,@RequestParam(value = "stats") String stats)
    {
        mallProductInfoService.uploadInfo(id, stats);
        return R.ok();
    }



    /*@RequiresPermissions("product:info:UpdateStats")
    @GetMapping("/UpdateStats")
    public R UpdateStats(@RequestParam String id,@RequestParam String stats){
        mallProductInfoService.UpdateStats(id,stats);
        return R.ok();
    }*/


    @GetMapping(value = "/result/{id}")
    public R<ProductInfoResponse> getResultInfo(@PathVariable("id") Long id)
    {
        return R.ok(mallProductInfoService.selectMallProductInfoById(id));
    }

    /**
     * 获取商品详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public R<ProductDetailsResponse> getProductResponse(@PathVariable("id") Long id){
        return R.ok(mallProductInfoService.selectProductDetailsById(id));
    }

    /**
     * 新增商品信息
     */
    @RequiresPermissions("product:info:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("商品添加")
    public AjaxResult add(@RequestBody @ApiParam("商品请求实体类") ProductInfoRequest productInfoRequest)
    {
        return toAjax(mallProductInfoService.insertMallProductInfo(productInfoRequest));
    }

    /**
     * 修改商品信息
     */
    @RequiresPermissions("product:info:edit")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductInfoRequest productInfoRequest)
    {
        return toAjax(mallProductInfoService.updateMallProductInfo(productInfoRequest));
    }

    /**
     * 删除商品信息
     */
    @RequiresPermissions("product:info:remove")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mallProductInfoService.deleteMallProductInfoByIds(ids));
    }
}
